/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.impl;

import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.ResourceConstants;

import doan.quanlycanbo.portlet.NoSuchdanhhieuException;
import doan.quanlycanbo.portlet.model.danhhieu;
import doan.quanlycanbo.portlet.service.base.danhhieuLocalServiceBaseImpl;

/**
 *
 * @author Nguyen Tuan Vinh
 */
public class danhhieuLocalServiceImpl extends danhhieuLocalServiceBaseImpl {
	
	public List<danhhieu> getAll() throws SystemException{
		return danhhieuPersistence.findAll();
	}
	
	public danhhieu addDanhHieu(danhhieu danhHieu1, long userId) throws SystemException{
		long madanhhieu = CounterLocalServiceUtil.increment(danhhieu.class.getName());
		danhhieu danhHieu2 = danhhieuPersistence.create(madanhhieu);
		danhHieu2.setGroupId(danhHieu1.getGroupId());
		danhHieu2.setCompanyId(danhHieu1.getCompanyId());
		danhHieu2.setLanguageId(danhHieu1.getLanguageId());
		danhHieu2.setTendanhhieu(danhHieu1.getTendanhhieu());
		
		try {
			resourceLocalService.addResources(danhHieu1.getCompanyId(), danhHieu1.getGroupId(), userId, 
					danhhieu.class.getName(), danhHieu2.getPrimaryKey(), false, true, true);
		} catch (PortalException e) {
			e.printStackTrace();
		}
		
		return danhhieuPersistence.update(danhHieu2, false);
	}
	
	public danhhieu updateDanhHieu(long madanhhieu, String tendanhhieu) throws SystemException, NoSuchdanhhieuException{
		danhhieu danhHieu = danhhieuPersistence.findByPrimaryKey(madanhhieu);
		danhHieu.setTendanhhieu(tendanhhieu);
		return danhhieuPersistence.update(danhHieu, false);
	}
	
	public danhhieu deleteDanhHieu(long madanhhieu) throws SystemException, NoSuchdanhhieuException,PortalException{
		danhhieu danhHieu = danhhieuPersistence.findByPrimaryKey(madanhhieu);
		return deleteDanhHieu(danhHieu);
	}
	
	public danhhieu deleteDanhHieu(danhhieu danhHieu) throws SystemException,PortalException{
		resourceLocalService.deleteResource(danhHieu.getCompanyId(), danhhieu.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, danhHieu.getPrimaryKey());
		return danhhieuPersistence.remove(danhHieu);
	}
}